package com.zzt.xytv;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ui.PlayerView;

public class PlayActivity extends AppCompatActivity implements PlayerManager.Listener {

    private String TAG = PlayActivity.class.getSimpleName();
    private PlayerView localPlayerView;
    private PlayerManager playerManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initStart();
    }

    public void initStart() {
        localPlayerView = findViewById(R.id.local_player_view);
        localPlayerView.requestFocus();
    }

    @Override
    public void onResume() {
        super.onResume();
        playerManager =
                new PlayerManager(
                        this,
                        localPlayerView,
                        this
                );
        playerManager.addItem(DemoUtil.SAMPLES.get(0));
        playerManager.selectQueueItem(0);
    }

    @Override
    public void onPause() {
        super.onPause();
        playerManager.release();
        playerManager = null;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event) || playerManager.dispatchKeyEvent(event);
    }

    @Override
    public void onQueuePositionChanged(int previousIndex, int newIndex) {

    }

    @Override
    public void onUnsupportedTrack(int trackType) {
        if (trackType == C.TRACK_TYPE_AUDIO) {
            showToast(R.string.error_unsupported_audio);
        } else if (trackType == C.TRACK_TYPE_VIDEO) {
            showToast(R.string.error_unsupported_video);
        }
    }

    private void showToast(int messageId) {
        Toast.makeText(getApplicationContext(), messageId, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "KeyEvent ：" + event.getKeyCode());
        switch (keyCode) {
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                Log.d(TAG, "播放暂停--->");
                break;
            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
                Log.d(TAG, "enter--->");
                break;
            case KeyEvent.KEYCODE_BACK:    //返回键
                Log.d(TAG, "back--->");
                exitApp();
                return true;   //这里由于break会退出，所以我们自己要处理掉 不返回上一层
            case KeyEvent.KEYCODE_SETTINGS: //设置键
                Log.d(TAG, "setting--->");
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:   //向下键
                /*    实际开发中有时候会触发两次，所以要判断一下按下时触发 ，松开按键时不触发
                 *    exp:KeyEvent.ACTION_UP
                 */
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    Log.d(TAG, "down--->");
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:   //向上键
                Log.d(TAG, "up--->");
                break;
            case KeyEvent.KEYCODE_0:   //数字键0
                Log.d(TAG, "0--->");
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
                Log.d(TAG, "left--->");
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:  //向右键
                Log.d(TAG, "right--->");
                break;
            case KeyEvent.KEYCODE_INFO:    //info键
                Log.d(TAG, "info--->");
                break;
            case KeyEvent.KEYCODE_PAGE_DOWN:     //向上翻页键
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                Log.d(TAG, "page down--->");
                break;
            case KeyEvent.KEYCODE_PAGE_UP:     //向下翻页键
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                Log.d(TAG, "page up--->");
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:   //调大声音键
                Log.d(TAG, "voice up--->");
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
                Log.d(TAG, "voice down--->");
                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
                Log.d(TAG, "voice mute--->");
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private long firstTime = 0;

    private void exitApp() {
        if ((System.currentTimeMillis() - firstTime) > 2000) {
            Toast.makeText(PlayActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
